<?php
namespace Home\Controller;
use Think\Controller;
class LathController extends Controller {
    public function index(){
        indexRvent();//测算首页执行事件
        cookie('orderidnum','Lath');
        $this->assign('titleName',titleName('Lath',1));//首页名称

        indexRequest($_REQUEST);//测算首页接收数据处理

        if($_REQUEST['discountQczh']){//趣测综合页优惠
            cookie('discountQczh',$_REQUEST['discountQczh']);
        }

        cnzz_pvuv('TH');//执行统计
        //渠道获取ID
        $channelID=obtain_channelID('TH');
        $this->assign('channelID',$channelID);

        $user_agent = $_SERVER['HTTP_USER_AGENT'];//用户使用的浏览器，操作系统等信息。
        if (strpos($user_agent, 'MicroMessenger') == false) {
            //非微信浏览器访问
            cookie('wxlogin',0);
        }else{
            cookie('wxlogin',1);
            if($_SERVER['HTTP_HOST']=='hy.yixueqm.com'){
                if($_REQUEST['openid']!=''){
                    cookie('openid',$_REQUEST['openid'],7100);
                    S('ticket',$_REQUEST['ticket'],7100);
                }
                $openid=cookie('openid');
                $wzurl=wxJsdkData();
                if(empty($openid)){
                    openidJump('Lath',cookie('channel'));
                }

                $noncestr=noncestr(15);
                $timestamp=time();
                $ticket= S('ticket');
                $signature=sha1("jsapi_ticket={$ticket}&noncestr={$noncestr}&timestamp={$timestamp}&url={$wzurl}");

                $this->assign('appid',cookie('APPID'));
                $this->assign('timestamp',$timestamp);
                $this->assign('noncestr',$noncestr);
                $this->assign('signature',$signature);
            }else{
                getToken();
                getTicket();
                $noncestr=noncestr(15);
                $timestamp=time();
                $ticket= S('ticket');
                $wzurl=wxJsdkData();
                $signature=sha1("jsapi_ticket={$ticket}&noncestr={$noncestr}&timestamp={$timestamp}&url={$wzurl}");

                $this->assign('appid',cookie('APPID'));
                $this->assign('timestamp',$timestamp);
                $this->assign('noncestr',$noncestr);
                $this->assign('signature',$signature);

                if(!empty($_REQUEST['code'])){
                    cookie('code',$_REQUEST['code'],7100);
                }
                getCode($wzurl);
                getOopenid();
            }
            wxShareUrl('TH','Lath');//分享地址获取
            cookie('uid',cookie('openid'));
        }

        //桃花运随机数
        csRandNum('lathcs',300000,499999);

        //半价
        $jieguoye=$_REQUEST['jieguoye'];
        banjia($jieguoye);


        $this->assign("orderidnum",'Lath');
        $this->display("Lath/ZM_lianaitaohuayun");
    }
    public function lath_zhifu(){
        $this->assign('titleName',titleName('Lath',2));//首页名称
        $strData=mb_substr(cookie('orderid'),0,2);
        if($strData=='QS')cookie('orderid',null);

        $user_agent = $_SERVER['HTTP_USER_AGENT'];//用户使用的浏览器，操作系统等信息。
        if (strpos($user_agent, 'MicroMessenger') == false) {
            //非微信浏览器访问
            cookie('wxlogin',0);
        }else{
            cookie('wxlogin',1);
            $this->assign('wxlogin',1);

            if($_SERVER['HTTP_HOST']=='hy.yixueqm.com'){

            }else{
                getToken();
                getTicket();
            }
            $noncestr=noncestr(15);
            $timestamp=time();
            $ticket= S('ticket');
            $wzurl=wxJsdkData();
            $signature=sha1("jsapi_ticket={$ticket}&noncestr={$noncestr}&timestamp={$timestamp}&url={$wzurl}");

            $this->assign('appid',cookie('APPID'));
            $this->assign('timestamp',$timestamp);
            $this->assign('noncestr',$noncestr);
            $this->assign('signature',$signature);

        }

        if($_REQUEST['username']){//返回操作
            cookie('znickname',I('post.username'),604800);
            cookie('zymd',mb_substr($_REQUEST['birthday'],2,10),7200);
            cookie('zhour',mb_substr($_REQUEST['birthday'],13,2),7200);
            cookie('zsex',$_REQUEST['gender'],604800);
            cookie('datetype',mb_substr($_REQUEST['birthday'],0,1));
        }
        if($_REQUEST['birthday']==''){
            $birthday='0-2018-08-08-8';
            cookie('zymd',$birthday);
        }

        if(!empty($_REQUEST['ordernum'])){//未支付订单
            $arrdata[0]=S('sm_h5test|'.$_REQUEST['ordernum']);
            $zymd=$arrdata[0]['year'].'-'.str_pad($arrdata[0]['month'],2,"0",STR_PAD_LEFT).'-'.str_pad($arrdata[0]['day'],2,'0',STR_PAD_LEFT);
            cookie('zymd',$zymd,604800);
            cookie('zhour',$arrdata[0]['hour'],604800);
            cookie('znickname',$arrdata[0]['username'],604800);
            cookie('zsex',$arrdata[0]['sex'],604800);
            cookie('datetype',$arrdata[0]['datetype']);

            cookie('orderid',$_REQUEST['ordernum'],604800);
        }

        $y=mb_substr(cookie('zymd'),0,4);//年
        $m=mb_substr(cookie('zymd'),5,2); //月
        $d=mb_substr(cookie('zymd'),8,2);//日
        $zyinli=yinli($y,$m,$d);
        cookie('zyangli',$y.'年'.$m.'月'.$d.'日'.' '.cookie('zhour').'时',604800);//阳历
        cookie('zyinli',$zyinli[0].'年'.$zyinli[1].$zyinli[2],604800);//阴历

        $data=cookie('orderid');//获取订单号
        if(empty($data)&&empty($_REQUEST['ordernum'])){
            $orderid='TH'.date('Ymd').mt_rand(100,999).date('His');
            cookie('orderid',$orderid,604800);
        }

        //创建订单
        $hour=cookie('zhour');if($hour=='')$hour=0;
        $shop=cookie('orderid');
        $znickname=cookie('znickname');if($znickname=='')$znickname='匿名';
        $zsex=cookie('zsex');if($zsex=='')$zsex=1;
        $channel=cookie('channel');
        $imei=cookie('uid');if(cookie('uid')==''){$imei=cookie('imei');}
        $y=mb_substr(cookie('zymd'),0,4);//年
        $m=mb_substr(cookie('zymd'),5,2); //月
        $d=mb_substr(cookie('zymd'),8,2);//日
        $datetype=cookie('datetype');if($datetype=='')$datetype=0;
        defaultPrice();

        channelPay($channel,'TH');//查询渠道价格

        if(cookie('couponPrice')){//是否有使用优惠券
            cookie('price',cookie('couponPrice'));
            $this->assign('discount',10);//优惠券价格
        }else{
            $this->assign('discount',0);//优惠券价格
        }

        if(cookie('discountQczh')){//是否有趣测优惠券
            $price=cookie('price');
            cookie('price',$price/2);
            $remark='discountQczh';
        }

        csPay($znickname,$channel);//测试价格判断

        $price=cookie('price');
        if(empty($_REQUEST['ordernum'])){
//            $arrdata=M()->query("select * from sm_h5test where ordernum='{$shop}'");
//            if(empty($arrdata)){
//                M()->query("insert into sm_h5test (ordernum,price,username,typeid,sex,status,datetype,year,month,day,hour,paykind,appuserid,ip,channel,date_remark)values(
//                                              '{$shop}','{$price}','{$znickname}',25,{$zsex},-1,{$datetype},{$y},{$m},{$d},{$hour},-1,'{$imei}','{$_SERVER['REMOTE_ADDR']}','{$channel}','{$remark}')");
//                $IndexController=A('Index');
//                $IndexController->subNumber('TH');
//            }
//            if(!cookie($_REQUEST['username'])){//返回操作
//                //更新订单
//                M()->query("update sm_h5test set username='{$znickname}',datetype='{$datetype}',year={$y},month={$m},day={$d},hour={$hour},sex={$zsex} where ordernum='{$shop}'");
//            }
            $arrdata=S('sm_h5test|'.$shop);
            if(empty($arrdata)){
                $smH5testinter=array(
                    'ordernum'=>$shop, 'price'=>$price, 'username'=>$znickname, 'typeid'=>25, 'sex'=>$zsex, 'createtime'=>date('Y-m-d H:i:m'),
                    'status'=>-1, 'datetype'=>$datetype, 'year'=>$y, 'month'=>$m, 'day'=>$d, 'hour'=>$hour, 'date_remark'=>$remark,
                    'appuserid'=>$imei, 'ip'=>$_SERVER['REMOTE_ADDR'], 'channel'=>$channel, 'auto_send_sms'=>0, 'place'=>0,
                );
                S('sm_h5test|'.$shop,$smH5testinter,2592000);//2592000为30天
                cacheZhiFuOrderid($shop);//支付页缓存订单操作

                $IndexController=A('Index');//Index控制器
                $IndexController->subNumber('TH');//提交订单次数统计
            }else if(!cookie($_REQUEST['username'])){//返回操作
                //更新订单
                $arrdata['username']=$znickname;$arrdata['datetype']=$datetype;
                $arrdata['year']=$y;$arrdata['month']=$m;$arrdata['day']=$d;$arrdata['sex']=$zsex;
                S('sm_h5test|'.$shop,$arrdata,2592000);//2592000为30天
            }
        }

        $zymd= cookie('zymd');
        if(cookie('zsex')==1){
            $zsex='男';
        }else{
            $zsex='女';
        }
        if($zymd!=''){
            zwmp($zymd,cookie('zhour'),$zsex,'zw');
            $this->assign('wxj',I('globals.wxj'));//五行局
            $this->assign('sizhu',I('globals.sizhu'));//Z四柱
            $this->assign('arryysz',I('globals.arryysz'));//四柱阴阳
            $this->assign('shengxiao',I('globals.shengxiao'));//生肖
            $this->assign('bzrizhu',I('globals.bzrizhu'));//日柱
            fortune(I('globals.shengxiao'),I('globals.bzrizhu'));
            $this->assign('lovefenxi',I('globals.lovefenxi'));//爱情分析
            $this->assign('arrtaohua',I('globals.arrtaohua'));//爱情桃花
            LAtaohua(I('globals.arrtaohua'),$zymd);
            $this->assign('hidetaohua',I('globals.hidetaohua'));//隐藏桃花
        }

        if (strpos($user_agent, 'MicroMessenger') == false) {
            //非微信浏览器访问
            if (strpos($user_agent, 'Windows') == true||strpos($user_agent,'Mac') == true) {
                //Windows 电脑访问
                if(channelSaoma($channel)){
                    $this->assign('pc',1);
                    $saomaUrl=wxsaoma('TH','恋爱桃花运',cookie('orderid'));//微信扫码回调参数
                    $this->assign('wxsaoma',$saomaUrl);
                }
            }
        }else{
            $xml=wxgzhzf('TH','恋爱桃花运',cookie('orderid'));//公众号支付回调参数
            $xmlstring = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
            $val = json_decode(json_encode($xmlstring),true);
            $noncestr=noncestr(15);//随机字符串
            $timestamp=time();//时间戳

            $stringA="appId=".cookie('APPID')."&nonceStr={$noncestr}&package=prepay_id={$val['prepay_id']}&signType=MD5&timeStamp={$timestamp}";
            $stringSignTemp=$stringA."&key=".cookie('wxKEY');
            $sign=strtoupper(md5($stringSignTemp));

            $this->assign('timeStamp',$timestamp);
            $this->assign('nonceStr',$noncestr);
            $this->assign('package','prepay_id='.$val['prepay_id']);
            $this->assign('signType','MD5');
            $this->assign('paySign',$sign);
            $this->assign('appId',cookie('APPID'));
        }

        $this->assign('csNameJump',U('Lath/jieguoye','',false).'?ordernum='.cookie('orderid'));//支付成功跳转地址
        $this->assign('channel',cookie('channel'));

        $this->assign('orderidnum',cookie('orderidnum'));
        $this->assign('sex',cookie('zsex'));
        $this->display("Lath/ZM_lianaitaohuayun_zhifu");
    }
    public function jieguoye(){
        jieguoInterface();//结果页公共函数
        $this->assign('titleName',titleName('Lath',3));//首页名称
        $channel=cookie('channel');
        $user_agent = $_SERVER['HTTP_USER_AGENT'];//用户使用的浏览器，操作系统等信息。
        if (strpos($user_agent, 'MicroMessenger') == false) {
            //非微信浏览器访问
            cookie('wxlogin',0);
        }else{
            cookie('wxlogin',1);
            $noncestr=noncestr(15);
            $timestamp=time();
            $ticket= S('ticket');
            $wzurl=wxJsdkData();
            $signature=sha1("jsapi_ticket={$ticket}&noncestr={$noncestr}&timestamp={$timestamp}&url={$wzurl}");

            $this->assign('appid',cookie('APPID'));
            $this->assign('timestamp',$timestamp);
            $this->assign('noncestr',$noncestr);
            $this->assign('signature',$signature);
        }


        $ordernum=$_REQUEST['ordernum'];
        $arrdata[0]=jieguoyeArrData($ordernum);

        $zymd=$arrdata[0]['year'].'-'.str_pad($arrdata[0]['month'],2,"0",STR_PAD_LEFT).'-'.str_pad($arrdata[0]['day'],2,'0',STR_PAD_LEFT);
        cookie('zymd',$zymd,604800);
        cookie('zhour',$arrdata[0]['hour'],604800);
        cookie('znickname',$arrdata[0]['username'],604800);
        cookie('zsex',$arrdata[0]['sex'],604800);

        $y=mb_substr(cookie('zymd'),0,4);//年
        $m=mb_substr(cookie('zymd'),5,2); //月
        $d=mb_substr(cookie('zymd'),8,2);//日
        $zyinli=yinli($y,$m,$d);
        cookie('zyangli',$y.'年'.$m.'月'.$d.'日'.' '.cookie('zhour').'时',604800);//阳历
        cookie('zyinli',$zyinli[0].'年'.$zyinli[1].$zyinli[2],604800);//阴历
        $zymd= cookie('zymd');
        cookie('orderid',$arrdata[0]['ordernum'],604800);


        jieguoyeRequest($arrdata[0]['status']);//结果页公共处理


        if($channel=='kryw'||$channel=='sjg'){
            $wzurl=wxJsdkData();
            $loopNum=loopSecond(cookie('orderid'));//循环读秒
            if($loopNum<120){
                $this->assign('wzurl',$wzurl);
                $this->display('Index/wait-loader');exit;
            }
        }

        if(cookie('zsex')==1){$zsex='男';
        }else{$zsex='女';
        }
        if($zymd!=''){
            zwmp($zymd,cookie('zhour'),$zsex,'zw');
            $this->assign('wxj',I('globals.wxj'));//五行局
            $this->assign('sizhu',I('globals.sizhu'));//Z四柱
            $this->assign('arryysz',I('globals.arryysz'));//四柱阴阳
            $this->assign('shengxiao',I('globals.shengxiao'));//生肖
            $this->assign('bzrizhu',I('globals.bzrizhu'));//日柱
            fortune(I('globals.shengxiao'),I('globals.bzrizhu'));
            $this->assign('lovefenxi',I('globals.lovefenxi'));//爱情分析
            $this->assign('arrtaohua',I('globals.arrtaohua'));//爱情桃花
            LAtaohua(I('globals.arrtaohua'),$zymd);
            $this->assign('hidetaohua',I('globals.hidetaohua'));//隐藏桃花
            $this->assign('thmjarr',I('globals.thmjarr'));//催桃花秘籍
            $this->assign('thysarr',I('globals.thysarr'));//近三月桃花运势
        }


        $this->assign('sex',cookie('zsex'));
        $this->display("Lath/ZM_lianaitaohuayun_zhifu_jieguoye");
    }
    public function pay(){//支付宝支付
        $this->assign('ordername1','恋爱桃花运');
        $this->assign('ordername2','知命-恋爱桃花运');
        $this->display("Index/pay");
    }
    public function zzhifu(){
        $data=M()->query("select status from sm_h5test where ordernum='".cookie('orderid')."'");
        $this->assign('status',$data[0]['status']);
        $this->display("Index/ZM_zzhifu");
    }
    public function paywx(){//H5微信支付
        H5ZMwxzhifu('TH','恋爱桃花运',cookie('orderid'),'Lath');//知命
    }

}